﻿//-------------------------------------
// WBFSSync - WBFSSync.exe
//
// Copyright 2009 Caian (ÔmΣga Frøst) <frost.omega@hotmail.com>
//
// WBFSSync is Licensed under the terms of the Microsoft Reciprocal License (Ms-RL)
//
// ThreadManager.cs
//
// Implementa uma classe the registra todas as requisições à ThreadPool do aplicativo
//
//-------------------------------------

using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Diagnostics;

namespace WBFSSync
{
    public static class ThreadManager
    {
        //----------------------
        public const int MaxThreads = 512;
        public static int NumCores { get { return Environment.ProcessorCount; } }
        //----------------------
        static Thread[] Threads = new Thread[MaxThreads];

        //---------------------- Procura por um indice vago
        public static void SetProcessAfinityToAllCores()
        {
            int affinity = 0;
            for (int index = 1; index <= Environment.ProcessorCount; index++)
                affinity |= index;

            Process CurrentProcess = Process.GetCurrentProcess();
            CurrentProcess.ProcessorAffinity = (IntPtr)(affinity);
        }

        //---------------------- Verifica se uma thread com uma determinada id já terminou
        public static bool ThreadReturned(String id)
        {
            for (int i = 0; i < MaxThreads; i++)
            {

                if ((Threads[i] != null) && (String.Compare(Threads[i].Name, id, true) == 0))
                    return !Threads[i].IsAlive;
            }

            return true;
        }

        //---------------------- Procura por um indice vago
        public static int SearchThreadSlot()
        {
            for (int i = 0; i < MaxThreads; i++)
            {

                if ((Threads[i] == null) || !Threads[i].IsAlive) return i;
            }

            return -1;
        }

        //---------------------- Conta quantas threads estão funcionando
        public static int CountBusyThreads()
        {
            int j = 0;
            for (int i = 0; i < MaxThreads; i++)
            {

                if ((Threads[i] != null) && Threads[i].IsAlive) j++;
            }

            return j;
        }

        //---------------------- Cancela todas as threads em funcionamento
        public static void AbortAllThreads()
        {
            for (int i = 0; i < MaxThreads; i++)
            {

                if ((Threads[i] != null) && Threads[i].IsAlive)
                    Threads[i].Abort();
            }
        }
        
        //---------------------- Inicia uma thread pelo ThreadPool e registra
        public static Thread LaunchThread(String id, ThreadStart routine)
        {
            int slot = SearchThreadSlot();
            if (slot < 0) return null;

            Threads[slot] = new Thread(routine);
            Threads[slot].Name = id;
            Threads[slot].Start();
            return Threads[slot];
        }

        //---------------------- Inicia uma thread com um objeto pelo ThreadPool e registra
        public static Thread LaunchThread(String id, ParameterizedThreadStart routine, Object o)
        {
            int slot = SearchThreadSlot();
            if (slot < 0) return null;

            Threads[slot] = new Thread(routine);
            Threads[slot].Name = id;
            Threads[slot].Start(o);
            return Threads[slot];
        }
    }
}
